/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.instrument.renderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

import demo.network.miscellaneous.instrument.attachment.KPIAttachment;
 
public class KPIRenderer extends JComponent implements TableCellRenderer{

	protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
	protected static Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");

	private Color color;
	private double proportion;

	public KPIRenderer(){
	}

    public void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	Dimension size = this.getSize();

    	GradientPaint gp = new GradientPaint(0, 0, Color.LIGHT_GRAY, 0, size.height/2, Color.white, true);
        ((Graphics2D)g).setPaint(gp);
    	g.fillRect(0, 0, size.width, size.height);

    	gp = new GradientPaint(0, 0, color, 0, size.height/2, Color.white, true);
        ((Graphics2D)g).setPaint(gp);
    	g.fillRect(0, 0, (int)(size.width * proportion), size.height);
    }

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
    	if(hasFocus){
    		this.setBorder(focusBorder);
    	}else{
    		this.setBorder(noFocusBorder);
    	}
    	String valueText = (String)value;
    	if(valueText == null){
    		color = Color.white;
    	}else{
    		for(int i=0; i<KPIAttachment.KEYS.length; i++){
    			String key = KPIAttachment.KEYS[i];
    			if(valueText.startsWith(key)){
    				proportion = Double.parseDouble(valueText.substring(key.length() + 1));
    				color = KPIAttachment.COLORS[i];
    			}
    		}
    	}
        this.setToolTipText(valueText);
		return this;
	}
}